﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using BySoft.Server.Business.UserCode;
using System.Web;


namespace BySoft.Web.Site.StockManage
{
    public partial class ReturnMatView : AuthBasePage
    {
        #region 属性定义
        private string m_strMainKey = "BillID";
        private static DataTable dtStock = null;
        private DataTable DtReturnMatData
        {
            get
            {
                if (null == Session["DtReturnMatData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtReturnMatData"];
                }
            }
            set
            {
                Session["DtReturnMatData"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("MatUseDetailID");//1 
            dt.Columns.Add("MaterialID");//2 
            dt.Columns.Add("MaterialName");//4
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("ChangeNum");//5 
            dt.Columns.Add("UseNums");//9
            dt.Columns.Add("LeftNums");//10
            dt.Columns.Add("StockNums");//11
            dt.Columns.Add("Nums");//12 
            dt.Columns.Add("MMNumber");//12 
            dt.Columns.Add("SingleMMNumber");//12 
            dt.Columns.Add("Remark");//13 
            dt.Columns.Add("IsNew");//14 
            dt.Columns.Add("IsDel");//14
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            txtWorkOrderNo.Attributes.Add("readonly", "true");
            txtScheduleID.Attributes.Add("readonly", "true");
            txtProductID.Attributes.Add("readonly", "true");
            txtProductName.Attributes.Add("readonly", "true");
            txtProductNum.Attributes.Add("readonly", "true");
            DtReturnMatData = null;
            SetControlAttr(new BsReturnmatEntity(), tBase);

            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsReturnMatBusiness.GetDataByID(id);
                this.lblBillID.Text = id;
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow row1 = dt.Rows[0];
                    this.lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", row1["BillDate"]);
                    this.txtWorkOrderNo.Text = CommonMethod.GetString(row1["WorkOrderNo"]);
                    hdScheduleDetailID.Value = CommonMethod.GetString(row1["ScheduleDetailID"]);
                    txtScheduleID.Text = CommonMethod.GetString(row1["ScheduleID"]);
                    txtProductID.Text = CommonMethod.GetString(row1["ProductID"]);
                    txtProductName.Text = CommonMethod.GetString(row1["ProductName"]);
                    txtProductNum.Text = CommonMethod.GetString(row1["ScheduleNum"]);
                    DataTable dtemp = DtReturnMatData.Copy();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow drow = dtemp.NewRow();
                        drow["BillDetailID"] = CommonMethod.GetString(dr["BillDetailID"]);//0
                        drow["MatUseDetailID"] = CommonMethod.GetString(dr["MatUseDetailID"]);//2 
                        drow["MaterialID"] = CommonMethod.GetString(dr["MaterialID"]);//5
                        drow["MaterialName"] = CommonMethod.GetString(dr["MaterialName"]);//6
                        drow["Specifications"] = CommonMethod.GetString(dr["Specifications"]);//7
                        drow["Unit"] = CommonMethod.GetString(dr["Unit"]);//8
                        drow["ChangeNum"] = CommonMethod.GetString(dr["ChangeNum"]);//9
                        drow["UseNums"] = CommonMethod.GetString(dr["UseNums"]);//9
                        drow["StockNums"] = CommonMethod.GetString(dr["StockNums"]);//9
                        drow["LeftNums"] = CommonMethod.GetString(dr["LeftNums"]);//10
                        drow["Nums"] = CommonMethod.GetString(dr["Nums"]);//11
                        drow["MMNumber"] = CommonMethod.GetString(dr["MMNumber"]);//13
                        drow["SingleMMNumber"] = CommonMethod.GetString(dr["SingleMMNumber"]);//13
                        drow["Remark"] = CommonMethod.GetString(dr["Remark"]);//13
                        drow["IsNew"] = "-1";//14 
                        drow["IsDel"] = "-1";//14
                        dtemp.Rows.Add(drow);
                    }
                    dtemp.AcceptChanges();
                    DtReturnMatData = dtemp.Copy();
                }
            }
            BindData();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.UpdatePanel1, "Refreashtab();");
        }
        private void BindData()
        {
            DataView dv = new DataView(DtReturnMatData);
            dv.RowFilter = "IsDel<>'1'";
            dv.Sort = " MatUseDetailID ";
            DataTable dtTemp = dv.ToTable();
            if (dtTemp.Rows.Count > 0)
            {
                grdResult.DataSource = dtTemp;
                grdResult.DataBind();
                SetControlAttr(new BsPurchasedetailEntity(), grdResult);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, CreateDT());
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }
    }
}
